//
//  BLAreaPickView.m
//  Byelone
//
//  Created by Qi Liu on 2021/2/7.
//

#import "BLAreaPickView.h"
#import "PGPickerView.h"

typedef void(^DatePickBlock)(NSDictionary * _Nullable dateDic);

@interface BLAreaPickView ()<PGPickerViewDelegate, PGPickerViewDataSource>
{
//    void(^B)(NSDictionary * dateDic);
}
@property(nonatomic,strong) UIView * baseView;
@property(nonatomic,strong) PGPickerView * pickView;
@property (nonatomic, strong) NSArray *bigArray;
@property(nonatomic,strong) UILabel * titleLabel;
@property(nonatomic,strong) UIButton * cancelBtn;
@property(nonatomic,strong) UIButton * sureBtn;
@property (nonatomic, assign) NSInteger provinceIndex,cityIndex;
@property(nonatomic,copy) DatePickBlock  dateBlock;

@end

@implementation BLAreaPickView


+(void)showAreaPickView:(void(^)(NSDictionary * dateDic))block{
    BLAreaPickView * pickView = [[BLAreaPickView alloc]initWithFrame:BL_WINDOW_FRAME];
    pickView.dateBlock = block;
    [BL_WINDOW addSubview:pickView];
}

-(NSArray * )bigArray{
    if (_bigArray == nil) {
        _bigArray = [BLUserManager shareUser].commonInfo.areaList;
    }
    return _bigArray;
}


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(disMissView)];
        [self addGestureRecognizer:tap];
    
        [self setUp];
    }
    return self;
}

-(void)disMissView{
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)sureClick{
    BLInfoAreaItem * area = [self.bigArray objectAtIndex:_provinceIndex];
    BLInfoCityItem * city =  [area.cityList objectAtIndex:_cityIndex];
    NSDictionary * areaDic = @{@"id":area.Id,
                          @"name":area.name
    };
    
    NSDictionary * cityDic = @{@"id":city.Id,
                          @"name":city.name
    };
    
    
    if (_dateBlock) {
        NSDictionary * dic = @{@"area":areaDic,@"city":cityDic};
        _dateBlock(dic);
    }
    [self disMissView];
}


#define baseViewH 260
#define blueViewW 320
#define pickViewW 120
-(void)setUp{
    self.backgroundColor = BL_COLOR_ALPHA;
    _provinceIndex = 0;
    _cityIndex = 0;
    
    UIView * baseView = [[UIView alloc]initWithFrame:CGRectMake(0, BL_WINDOW_HEIGHT-baseViewH, BL_WINDOW_WIDTH, baseViewH)];
    baseView.backgroundColor = [UIColor whiteColor];
    _baseView = baseView;
    [self addSubview:baseView];

    
    UIView * blueView = [[UIView alloc]init];
    blueView.backgroundColor = [UIColor chColorWithHexString:@"#E7E8FF"];
    blueView.frame = CGRectMake((BL_WINDOW_WIDTH - blueViewW)/2, (CGRectGetHeight(baseView.frame) - 34)/2, 320, 34);
    blueView.layer.cornerRadius = 10;
    blueView.layer.masksToBounds = YES;
    [baseView addSubview:blueView];
    
    UIButton * cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    cancleBtn.titleLabel.textColor = [UIColor whiteColor];
    [cancleBtn setBackgroundColor:[UIColor chColorWithHexString:@"#C3C3C3"]];
    cancleBtn.layer.cornerRadius = 8.75;
    cancleBtn.layer.masksToBounds = YES;
    [cancleBtn addTarget:self action:@selector(disMissView) forControlEvents:UIControlEventTouchUpInside];
    _cancelBtn = cancleBtn;
    [baseView addSubview:cancleBtn];
    
    
    UILabel * titleLable = [[UILabel alloc]init];
    titleLable.text = @"选择预约时间";
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.font = [UIFont systemFontOfSize:12];
    titleLable.textColor = [UIColor chColorWithHexString:@"#555555"];
    [baseView addSubview:titleLable];
    _titleLabel = titleLable;
    
    
    UIButton * sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    sureBtn.titleLabel.textColor = [UIColor whiteColor];
    [sureBtn setBackgroundColor:[UIColor chColorWithHexString:@"#6C72F5"]];
    sureBtn.layer.cornerRadius = 8.75;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    _sureBtn = sureBtn;
    [baseView addSubview:sureBtn];
    
    
    _pickView = [[PGPickerView alloc]initWithFrame:CGRectMake(2, (CGRectGetHeight(baseView.frame)-pickViewW)/2, BL_WINDOW_WIDTH, pickViewW)];
    _pickView.delegate = self;
    _pickView.dataSource = self;
    _pickView.type = PGPickerViewLineTypelineSegment;
    
    [_pickView selectRow:0 inComponent:0 animated:true];
    [_pickView selectRow:0 inComponent:1 animated:true];
//    [_pickView selectRow:3 inComponent:2 animated:true];
    _pickView.rowHeight = 45;
    _pickView.isHiddenMiddleText = false;
    //设置线条的颜色
    _pickView.lineBackgroundColor = [UIColor clearColor];
    //设置选中行的字体颜色
    _pickView.textColorOfSelectedRow = [UIColor chColorWithHexString:@"#6C72F5"];
    //设置未选中行的字体颜色
    _pickView.textColorOfOtherRow = [UIColor chColorWithHexString:@"#888888"];
    [baseView addSubview:_pickView];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _cancelBtn.frame = CGRectMake(22, 18.5, 40, 20);
    _titleLabel.frame = CGRectMake(0, 18.5, 150, 20);
    _titleLabel.middleX = _baseView.middleX;
    _sureBtn.frame = CGRectMake(BL_WINDOW_WIDTH-62, 18.5, 40, 20);
}


#pragma UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(PGPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(PGPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        return self.bigArray.count;
    }else if (component == 1){
        BLInfoAreaItem * area = [self.bigArray objectAtIndex:_provinceIndex];
        return  area.cityList.count;
    }
    return 0;
}
#pragma UIPickerViewDelegate
- (nullable NSString *)pickerView:(PGPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        BLInfoAreaItem * area = [self.bigArray objectAtIndex:row];
        return area.name;
    }
    if (component == 1) {
        BLInfoAreaItem * area = [self.bigArray objectAtIndex:_provinceIndex];
        BLInfoCityItem * city =  [area.cityList objectAtIndex:row];
        return city.name;
    }
    return @"";
}

- (void)pickerView:(PGPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"row = %ld component = %ld", row, component);
    if (component ==0) {
        [pickerView reloadComponent:1 refresh:YES];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        _provinceIndex = row;
        
    }else if (component ==1){
        _cityIndex = row;
//        [pickerView reloadComponent:2 refresh:YES];
//        [pickerView selectRow:0 inComponent:2 animated:YES];
    }
}


@end
